import paddle
import numpy as np
import matplotlib.pyplot as plt


infer_results = []
groud_truths = []
valid_dataset = paddle.text.UCIHousing(mode='test')
infer_loader = paddle.io.DataLoader(valid_dataset, batch_size=200)

infer_net = paddle.nn.Linear(13, 1)
param = paddle.load('fit_a_line.pdparams')
infer_net.set_dict(param)


data = next(infer_loader())
inputs = paddle.to_tensor(data[0])
results = infer_net(inputs)

#绘制真实值和预测值对比图
def draw_infer_result(groud_truths, infer_results):
    title='Boston'
    plt.title(title, fontsize=24)
    x = np.arange(1,20) 
    y = x
    plt.plot(x, y)
    plt.xlabel('ground truth', fontsize=14)
    plt.ylabel('infer result', fontsize=14)
    plt.scatter(groud_truths, infer_results, color='green',label='training cost') 
    plt.grid()
    plt.show()


for idx, item in enumerate(zip(results, data[1])):
    print("Index:%d, Infer Result: %.2f, Ground Truth: %.2f" % (idx, item[0], item[1]))
    infer_results.append(item[0].numpy()[0])
    groud_truths.append(item[1].numpy()[0])
    
draw_infer_result(groud_truths, infer_results)